package com.ca.ridersclubapplication.view.bar;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.ca.ridersclubapplication.R;

public class BaseTitleBar extends ConstraintLayout {

    private String title;

    public ImageView backView;
    public TextView titleView;

    public BaseTitleBar(@NonNull Context context) {
        super(context);
        initView(context, null);
    }

    public BaseTitleBar(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context, attrs);
    }

    public BaseTitleBar(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs);
    }

    private void initView(Context context, AttributeSet attrs) {
        View view = LayoutInflater.from(context).inflate(R.layout.base_title_bar_style, this);
        backView = view.findViewById(R.id.iv_back);
        titleView = view.findViewById(R.id.tv_title);

        if (attrs != null) {
            TypedArray array = context.getTheme().obtainStyledAttributes(attrs, R.styleable.BaseTitleBar, 0, 0);
            title = array.getString(R.styleable.BaseTitleBar_btb_title);

            array.recycle();
        }

        setTitle(title);
    }

    public void setTitle(String title) {
        this.title = title == null ? "" : title;
        titleView.setText(this.title);
    }

    public String getTitle() {
        return titleView.getText().toString();
    }
}
